package com.lily.ssm.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.lily.ssm.model.Pager;
import com.lily.ssm.model.Product;
import com.lily.ssm.model.ProductClass;
import com.lily.ssm.service.ProductService;

@Controller
public class ProductController {
	@Autowired
	private ProductService productService;
	
	/*
	 * 
	 */
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public String add(Model model) {
		List<ProductClass> productClasses = productService.getTwoList();
		System.out.println(productClasses.size());
		model.addAttribute("productClasses", productClasses);
		return "manage/product-add";
	}
	
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public String add(Product product,MultipartFile photo) throws IOException{
		FileOutputStream fos = new FileOutputStream("C:\\Users\\11791\\Workspaces\\MyEclipse Professional 2014\\ssm Maven Webapp\\src\\main\\webapp\\images\\product\\"+photo.getOriginalFilename());
		FileCopyUtils.copy(photo.getInputStream(), fos);
		product.setProductImg(photo.getOriginalFilename());
		productService.insertProdut(product);
		return "redirect:/productList.do";
	}
	
	@RequestMapping("/productList.do")
	public String pageList(Pager<Product> pager,Model model){
		//分页查询数据
		List<Product> products = productService.getProduct(pager.getOffset());
		//查询所有二级分类
		List<ProductClass> productClasses = productService.getTwoList();
		//查询共有Product多少条数据，存入Pager中
		pager.setCount(productService.getCount());
		//判断接受的pageIndex是否为0，为0的话赋予默认值1
		int index = pager.getPageIndex();
		index = index == 0 ? 1 :index;
		//将返回的pagerIndex存入pager中
		pager.setPageIndex(index);
		//将分页查询的数据存入pager中
		pager.setDatas(products);
		//将查询到的所有二级分类传到页面中
		model.addAttribute("productClasses", productClasses);
		//将pager传到页面
		model.addAttribute("pager", pager);
		return "manage/product";
	}
	
	@RequestMapping(value="/update.do",method=RequestMethod.GET)
	public String update(int productId,Model model){
		List<ProductClass> productClasses = productService.getTwoList();
		Product product = productService.selectProduct(productId);
		model.addAttribute("product", product);
		model.addAttribute("productClasses", productClasses);
		System.out.println(product.getClassId());
		return "manage/product-modify";
	}
	
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public String update(Product product,MultipartFile photo) throws IOException{
		if(photo.getSize() != 0){
			FileOutputStream fos = new FileOutputStream("E:\\apache-tomcat-8.5.13\\webapps\\ssm\\images\\product\\"+photo.getOriginalFilename());
			FileCopyUtils.copy(photo.getInputStream(), fos);
			product.setProductImg(photo.getOriginalFilename());
		}
		productService.updateProduct(product);
		File file=new File("");
	    String abspath=file.getAbsolutePath();
		System.out.println(abspath);
		return "redirect:/productList.do";
	}
	
	@RequestMapping("/delete.do")
	public String delete(int productId,Pager<Product> pager,Model model){
		productService.deleteProduct(productId);
		model.addAttribute("pager", pager);
		return "redirect:/productList.do";
	}
	
	@RequestMapping("/select.do")
	public String select(int classId,String productName,Pager<Product> pager,Model model){
		Map<String, Object> hashmap = new HashMap<>();
		hashmap.put("classId", classId);
		hashmap.put("productName", productName);
		hashmap.put("offset", pager.getOffset());
		// 分页查询数据
		List<Product> products = productService.getProductListById(hashmap);
		// 查询所有二级分类
		List<ProductClass> productClasses = productService.getTwoList();
		// 查询共有Product多少条数据，存入Pager中
		pager.setCount(productService.getSelectCount(hashmap));
		// 判断接受的pageIndex是否为0，为0的话赋予默认值1
		int index = pager.getPageIndex();
		index = index == 0 ? 1 : index;
		// 将返回的pagerIndex存入pager中
		pager.setPageIndex(index);
		// 将分页查询的数据存入pager中
		pager.setDatas(products);
		//将查询条件productName传到页面
		model.addAttribute("productName", productName);
		// 将查询到的所有二级分类传到页面中
		model.addAttribute("productClasses", productClasses);
		model.addAttribute("pager", pager);
		return "manage/product";
	}
}
